import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
import Vue from "vue";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    count: 0,
    user: {},
    restaurantId: null,
    collectionList: []
  },
  plugins: [createPersistedState()],
  getters: {
    count: state => state.count,
    uid: state => state.user.id,
    username: state => state.user.username,
    userType: state => state.user.type,
    rid: state => state.restaurantId,
    collection: state => state.collectionList
  },
  mutations: {
    increment (state) {
      state.count++
    },
    setUser (state, user) {
      state.user=user
    },
    setWallet (state, money) {
      state.user.wallet=money
    },
    setRestaurantId (state, rid) {
      state.restaurantId=rid
    },
    setCollectionList (state, list) {
      state.collectionList=list
    },
    resetState (state) {
      state.user={};
      state.restaurantId=null;
    }
  },
  actions: {
    increment ({ commit }) {  // 解构参数
      commit('increment')
    }
  }
});
